package com.example.freeengine.wallpaper.fragment.cyclerefresh.albumlist;

import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.PopupMenu;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import com.example.freeengine.R;
import com.example.freeengine.wallpaper.fragment.WallPaperViewModel;
import com.example.freeengine.wallpaper.persistence.entity.WallPaperCycleRefreshAlbumItem;
import com.example.freeengine.wallpaper.persistence.repository.WallPaperCycleRefreshRepository;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

/**
 * @author Terry Deng
 */
public class AlbumPictureListAdapter extends ListAdapter<WallPaperCycleRefreshAlbumItem, AlbumPictureListAdapter.MyViewHolder> {

    private static final String TAG = AlbumPictureListAdapter.class.getSimpleName();
    Context context;
    WallPaperViewModel wallPaperViewModel;
    WallPaperCycleRefreshRepository wallPaperCycleRefreshRepository;

    static DiffUtil.ItemCallback DIFF_CALLBACK = new DiffUtil.ItemCallback<WallPaperCycleRefreshAlbumItem>() {
        @Override
        public boolean areItemsTheSame(@NonNull WallPaperCycleRefreshAlbumItem oldItem, @NonNull WallPaperCycleRefreshAlbumItem newItem) {
            return oldItem.getId().equals(newItem.getId());
        }

        @Override
        public boolean areContentsTheSame(@NonNull WallPaperCycleRefreshAlbumItem oldItem, @NonNull WallPaperCycleRefreshAlbumItem newItem) {
            return oldItem.equals(newItem);
        }
    };
    public AlbumPictureListAdapter(){
        super(DIFF_CALLBACK);
    }

    public AlbumPictureListAdapter(Context context){
        super(DIFF_CALLBACK);
        this.context = context;
    }

    public AlbumPictureListAdapter(Context context, WallPaperViewModel wallPaperViewModel){
        super(DIFF_CALLBACK);
        this.context = context;
        this.wallPaperViewModel = wallPaperViewModel;
    }

    public AlbumPictureListAdapter(Context context, WallPaperViewModel wallPaperViewModel, WallPaperCycleRefreshRepository wallPaperCycleRefreshRepository){
        super(DIFF_CALLBACK);
        this.context = context;
        this.wallPaperViewModel = wallPaperViewModel;
        this.wallPaperCycleRefreshRepository = wallPaperCycleRefreshRepository;
    }

    @NonNull
    @NotNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        return new MyViewHolder(layoutInflater.inflate(R.layout.wallpaper_picture_list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull MyViewHolder holder, int position) {

        // Drawable drawable = Drawable.createFromPath(pictureInfoList.get(position).getPath());
        // holder.ivPicture.getLayoutParams().width = drawable.getIntrinsicWidth();
        holder.ivPicture.getLayoutParams().height = 200;

        WallPaperCycleRefreshAlbumItem data = getItem(position);

        File imageFile = new File(data.getPicturePath());
        if (imageFile.exists()){
            Log.i(TAG, "onBindViewHolder: " + Uri.parse(data.getPicturePath()));
            holder.ivPicture.setImageURI(Uri.fromFile(imageFile));
        }

        holder.ivItemMenu.setOnClickListener(v -> {
            PopupMenu popup = new PopupMenu(context, v);
            MenuInflater inflater = popup.getMenuInflater();
            inflater.inflate(R.menu.menu_wallpaper_cyclerefresh_album_picture_list_item, popup.getMenu());
            popup.show();

            popup.setOnMenuItemClickListener(item -> {
                switch (item.getItemId()){
                    case R.id.wallpaper_cyclerefresh_album_picture_delete: {
                        try {
                            // 不能new一个wallPaperCycleRefreshRepository来执行
                            wallPaperCycleRefreshRepository.deleteAlbumItem(data);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return true;
                    }
                    default:
                        return false;
                }
            });
        });

    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        public ImageView ivPicture, ivItemMenu;
        public MyViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            ivPicture = itemView.findViewById(R.id.iv_picture);
            ivItemMenu = itemView.findViewById(R.id.iv_item_menu);
        }
    }

}
